using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public class MainMenuPanel : MonoBehaviour
    {
        public Button btnChangeUser;//更改用户
        public Button btnMenu;//选项
        public Button btnHelp;//帮助
        public Button btnQuit;//退出
        public TextMeshProUGUI curUserName;
        public TextMeshProUGUI level;

        private void Awake()
        {
            btnChangeUser.onClick.AddListener(OnBtnChangeUser);
            btnMenu.onClick.AddListener(OnBtnMenu);
            btnHelp.onClick.AddListener(OnBtnHelp);
            btnQuit.onClick.AddListener(OnBtnQuit);

            AudioManager.Instance.PlayMusic("bgm2");

            if (DataManager.LoadClientData() == null)
            {
                DataManager.SaveClientData(new ClientData("测试"));
                DataManager.SaveUserData(new UserData("测试", 1));
            }
            curUserName.text = DataManager.LoadClientData().curUserName;
        }

        // 注册事件监听器
        private void OnEnable()
        {
            UserEvents.onClient += UserClient;
        }

        // 注销事件监听器
        private void OnDisable()
        {
            UserEvents.onClient -= UserClient;
        }
        private void UserClient(ClientData clientData) 
        {
            curUserName.text = clientData.curUserName;
            level.text = DataManager.LoadUserData(clientData.curUserName).level.ToString();
        }

        //打开用户列表
        public void OnBtnChangeUser()
        {
            BasePanel basePanel = UIManager.Instance.OpenPanel(UIConst.UserListPanel);
            // basePanel.gameObject.GetComponent<UserListPanel>().RefreshMainPanel();
        }

        public void OnBtnMenu()
        {
            UIManager.Instance.OpenPanel(UIConst.MenuPanel);
        }

        public void OnBtnHelp()
        {
            UIManager.Instance.OpenTips("功能正在火速开发中，敬请期待");
        }

        public void OnBtnQuit()
        {
            UIManager.Instance.OpenTips("功能正在火速开发中，敬请期待");
        }
    }
}
